﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.Data;

using HA.ModFlow.Infrastructure;


using System.ComponentModel;

using System.Collections.ObjectModel;


namespace HA.ModFlow.Application
{
   public class TimeStepViewModel:HViewModel
    {

       public TimeStepViewModel()
       {



       
 
       }





       bool _isNormalDataSet = true;

        DSGrid3d _dsGrid;

        public DSGrid3d DsGrid
        {
            get { return _dsGrid; }
            set {


             
                _dsGrid = value;

                this.OnSetDsGrid();
            
            
            }
        }



       //加入向量的考虑

        DSVectorGrid3d _vectorGrid;

        public DSVectorGrid3d VectorGrid
        {
            get { return _vectorGrid; }
            set { _vectorGrid = value;


            this.OnSetVectorGrid();
            
            }
        }


       protected void  OnSetVectorGrid()
       {




           //注明这里是Vector数据
           this._isNormalDataSet=false;




              int displayIndex = 0;

            if (this.SelectedStep != null)
            {
                int oldIndex = this.SelectedStep.Index;



                if (oldIndex < this._displayList.Count && oldIndex > -1)
                {

                    displayIndex = oldIndex;


                }




            }
             


            this._displayList.Clear();


            //考虑空值情况
            if (this._vectorGrid==null)
            {
                return;

                
            }



            if (this._vectorGrid.Count > 0)
            {





                int i = 0;

                foreach (CellCenterVector3dDS item in this._vectorGrid.Collecion)
                {
                    PCStep step = new PCStep(i, (i + 1).ToString() + " " + item.EllapseTime.ToString("f2"));

                    this._displayList.Add(step);

                    i++;

                }



                //默认选中第一个元素，或者是前一个元素



                ///第二次判断是否值大小是否在区间内

                if (displayIndex > this._displayList.Count - 1)
                {

                    displayIndex = 0;


                }




                this.SelectedStep = this._displayList[displayIndex];


            }


       }

        




        PCStep _selectedStep;

        public PCStep SelectedStep
        {
            get { return _selectedStep; }
            set { 
                
                _selectedStep = value;

                this.OnSetStep();


                this.OnPropertyChanged("SelectedStep");
            
            
            }
        }



        protected void OnSetStep()
        {

            if (this._selectedStep!=null)
            {

                ///正常的数据集，
                if (this._isNormalDataSet)
                {

                    CellCenterDataSet3d data = this._dsGrid.GetDataSet(this._selectedStep.Index);

                    if (data!=null)
                    {

                        ProjectService.DefaultService.Project.CurrentDSData = data;
                        
                    }

                   


                }

                else
                {

                    //向量数据集

                   CellCenterVector3dDS vector=  this._vectorGrid.GetDataSet(this._selectedStep.Index);

                   if (vector!=null)
                   {
                       ProjectService.DefaultService.Project.CurentFlowVectorData = vector;
                       
                   }

                  


 

                }

          

                
            }

         




 
        }


        ObservableCollection<PCStep> _displayList = new ObservableCollection<PCStep>();

        public ObservableCollection<PCStep> DisplayList
        {
            get { return _displayList; }
            set { _displayList = value; }
        }


        protected void OnSetDsGrid()
        {
            this._isNormalDataSet = true;

            int displayIndex = 0;

            if (this.SelectedStep != null)
            {
                int oldIndex = this.SelectedStep.Index;



                if (oldIndex < this._displayList.Count && oldIndex > -1)
                {

                    displayIndex = oldIndex;


                }




            }
             


            this._displayList.Clear();


            //考虑空值情况
            if (this._dsGrid==null)
            {
                return;

                
            }



            if (this._dsGrid.Count>0)
            {
               

              
                  

                    int i = 0;

                    foreach (CellCenterDataSet3d item in this._dsGrid.LstValue)
                    {
                        //PCStep step = new PCStep(i, (i+1).ToString()+" "+item.Label);

                        PCStep step = new PCStep(i, (i + 1).ToString() + " " + item.EllapseTime.ToString ("f2"));

                        this._displayList.Add(step);

                        i++;

                    }



                     //默认选中第一个元素，或者是前一个元素



                ///第二次判断是否值大小是否在区间内

                    if (displayIndex>this._displayList.Count-1)
                    {

                        displayIndex = 0;

                        
                    }




                     this.SelectedStep = this._displayList[displayIndex];

                  







                //}


                
            }

        
          
 
        }
 


    }


   public class PCStep
   {
       public PCStep(int index, string name)
       {
           this._index = index;

           this._name = name;


       }

       int _index;

       public int Index
       {
           get { return _index; }
           set { _index = value; }
       }


       string _name;

       public string Name
       {
           get { return _name; }
           set { _name = value; }
       }


   }
}
